<?php
/**
 * @Name: RoleController.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace App\Http\Controllers\Admin\Rbac;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Exceptions\ThrowException;
use App\Http\Logic\Rbac\RoleLogic;
use App\Http\Search\Rbac\RoleSearch;
use App\Http\Requests\StatusRequest;
use App\Http\Requests\Rbac\RoleSaveRequest;
use App\Http\Requests\Rbac\RoleNodeRequest;

class RoleController
{

    /**
     * @Desc 列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author yashuai<1762910894@qq.com>
     */
    public function index(Request $request)
    {
        $result = RoleSearch::instance($request->all())->index();
        return _success($result);
    }

    /**
     * @Desc 添加
     * @param RoleSaveRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function create(RoleSaveRequest $request)
    {
        $result = RoleLogic::instance($request->getValidationData())->create();
        return _success($result);
    }

    /**
     * @Desc 编辑
     * @param RoleSaveRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function update(RoleSaveRequest $request)
    {
        $validator = [
            'RoleID' => 'required|integer'
        ];
        $result    = RoleLogic::instance($request->getValidationData($validator))->update();
        return _success($result);
    }

    /**
     * @Desc 修改状态
     * @param StatusRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function status(StatusRequest $request)
    {
        $result = RoleLogic::instance($request->getValidationData())->status();
        return _success($result);
    }

    /**
     * @Desc 权限的菜单节点
     * @param RoleNodeRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function node(RoleNodeRequest $request)
    {
        $data = $request->getValidationData();
        return DB::transaction(function () use ($data) {
            RoleLogic::instance($data)->node();
            return _success();
        });
    }

    /**
     * @Desc 下拉框数据
     * @return \Illuminate\Http\JsonResponse
     * @author yashuai<1762910894@qq.com>
     */
    public function select()
    {
        $result = RoleSearch::instance()->select();
        return _success($result);
    }

}
